package com.gae101.service;

import java.io.UnsupportedEncodingException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;

public class MailServiceImpl implements MailService {

	private static final String DOMAIN = "a3-cctv.appspot.com";

	@Override
	public void send(Long id, String recipient, String... recipients) throws ServletException {
		Properties props = new Properties();
		Session session = Session.getDefaultInstance(props, null);
		try {
			StringBuilder sb = new StringBuilder();
			sb.append("<img src=\"http://").append(DOMAIN).append("/getImage?id=").append(id).append("\" width=\"100%\" />");
			String msgBody =  sb.toString();
			Message msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress("daclouds@gmail.com", "A3-CCTV Admin"));
			msg.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
			for (String another : recipients) {
				msg.addRecipient(Message.RecipientType.TO, new InternetAddress(another));
			}
			msg.setSubject("New Photo added");
			msg.setContent(msgBody, "text/html");
			Transport.send(msg);
		} catch (MessagingException e) {
			throw new ServletException(e);
		} catch (UnsupportedEncodingException e) {
			throw new ServletException(e);
		}
	}
}
